import { BasicColumn } from '/@/components/Table';
import { FormSchema } from '/@/components/Table';
import { setNameValue } from '/@/components/BpmnChart/src/components/Panel/bo-utils/nameUtil';
import { computed } from 'vue';
import { getDictTypeListApi } from '/@/api/admin/dict';
import { DICT } from '/@/enums/dict';

import FieldPerms from './base_settings/FieldPerms.vue';
import OtherSettings from './base_settings/OtherSettings.vue';

import { UpdateTypeList } from '/@/enums/modelEnum';

import modelerStore from '/@/store/modules/bpmnModeler';
const modeler = modelerStore();

export const messageListSchema: BasicColumn[] = [
  {
    title: '消息ID',
    dataIndex: 'id',
  },
  {
    title: '消息名称',
    dataIndex: 'name',
  },
];
export const buttonListSchema: BasicColumn[] = [
  {
    title: '按钮名称',
    dataIndex: 'name',
  },
];
export const signalListSchema: BasicColumn[] = [
  {
    title: '信号ID',
    dataIndex: 'id',
  },
  {
    title: '信号名称',
    dataIndex: 'name',
  },
];
export const executionListenerSchema: BasicColumn[] = [
  {
    title: '事件类型',
    dataIndex: 'modelName',
    width: 110,
  },
  {
    title: '监听器类型',
    dataIndex: 'modelCode',
    width: 110,
  },
];
export const extendedAttributeSchema: BasicColumn[] = [
  {
    title: '属性名',
    dataIndex: 'modelName',
    width: 110,
  },
  {
    title: '属性值',
    dataIndex: 'modelCode',
    width: 110,
  },
];

export const basicSchemas: FormSchema[] = [
  {
    field: 'bpmnId',
    component: 'Input',
    label: '任务Id',
    colProps: {
      span: 24,
    },
    componentProps: {
      disabled: true,
    },
  },
  {
    field: 'activityName',
    component: 'Input',
    label: '任务名称',
    colProps: {
      span: 24,
    },
    componentProps: {
      onBlur: (e) => {
        setNameValue(modeler.getActive, { name: e.target._value || '' });
        //modeler.setActivitiesItem(modeler.getActiveId, 'activityName', e.target._value);
      },
    },
  },

  {
    field: 'rangeType',
    label: '限制范围',
    component: 'ApiSelect',
    colProps: {
      span: 24,
    },
    componentProps: {
      api: getDictTypeListApi,
      valueField: 'intValue',
      params: {
        type: DICT.BPMN_RANGE_TYPE,
      },
      onChange: (e) => {
        setTimeout(() => {
          modeler.setActivitiesItem(modeler.getActiveId, 'rangeType', e);
        });
      },
    },
  },
  {
    field: 'participantRange',
    label: '限制内容',
    component: 'InputTextArea',
    colProps: { span: 24 },
    componentProps: {
      onBlur: (e) => {
        modeler.setActivitiesItem(modeler.getActiveId, 'participantRange', e.target._value);
      },
    },
  },

  // {
  //   field: 'rangeComponentType',
  //   label: '选择范围组件',
  //   component: 'ApiSelect',
  //   colProps: {
  //     span: 24,
  //   },
  //   componentProps: {
  //     api: async (params) => {
  //       const ret = await getDictTypeListApi(params);
  //       const filterArr = [14, 16, 17, 18, 19, 20, 21, 22, 23];
  //       return ret.filter((i) => filterArr.includes(i.intValue));
  //     },
  //     valueField: 'intValue',
  //     params: {
  //       type: DICT.COMPONENT_TYPE,
  //     },
  //     onChange: (e) => {
  //       setTimeout(() => {
  //         modeler.setActivitiesItem(modeler.getActiveId, 'rangeComponentType', e);
  //       });
  //     },
  //   },
  // },
  {
    field: 'summary',
    component: 'Input',
    label: '别名',
    colProps: {
      span: 24,
    },
    componentProps: {
      onBlur: (e) => {
        modeler.setActivitiesItem(modeler.getActiveId, 'summary', e.target._value);
        //modeler.setActivitiesItem(modeler.getActiveId, 'activityName', e.target._value);
      },
    },
  },
  {
    field: 'baseInfo.responseChoices',
    component: 'Input',
    label: '意见选项',
    colProps: {
      span: 24,
    },
    componentProps: {
      onBlur: (e) => {
        modeler.setActivitiesItem(modeler.getActiveId, 'opinion', e.target._value);
      },
    },
  },

  {
    field: `rights`,
    component: 'Select',
    label: '权限设置',
    colProps: {
      span: 24,
    },
    componentProps: {
      mode: 'multiple',
      options: modeler.configuration?.rights?.map((i) => ({
        label: i.rightName,
        value: i.rightId,
      })),
      onChange: (e) => {
        setTimeout(() => {
          modeler.setActivitiesItem(modeler.getActiveId, 'rights', e);
        });
      },
    },
  },
  {
    field: `allowRecallFlag`,
    component: 'Switch',
    label: '是否允许撤回',
    colProps: {
      span: 24,
    },
    componentProps: {
      checkedValue: 0,
      unCheckedValue: 1,
      onChange: (e) => {
        setTimeout(() => {
          modeler.setActivitiesItem(modeler.getActiveId, 'allowRecallFlag', e);
        });
      },
    },
  },
  {
    field: `verifySignatureFlag`,
    component: 'Switch',
    label: '是否验证签名',
    colProps: {
      span: 24,
    },
    componentProps: {
      checkedValue: 1,
      unCheckedValue: 0,
      onChange: (e) => {
        setTimeout(() => {
          modeler.setActivitiesItem(modeler.getActiveId, 'verifySignatureFlag', e);
        });
      },
    },
  },
  {
    field: 'activityType',
    component: 'Select',
    label: '步骤类型',
    colProps: {
      span: 24,
    },
    componentProps: (values) => {
      const startFlag = modeler.getActivities.some((i) => i.activityType === 0);

      return {
        options: [
          {
            label: '开始类型',
            value: 0,
            disabled: startFlag,
          },
          {
            label: '任务类型',
            value: 1,
          },
          {
            label: '通知类型',
            value: 2,
          },
          // {
          //   label: '分支类型',
          //   value: 5,
          // },
          // {
          //   label: '启动类型',
          //   value: -1,
          // },
        ],
        onChange: (e) => {
          setTimeout(() => {
            if (e === 0) {
              modeler.setActivitiesItem(modeler.getActiveId, 'activityId', 1);
              modeler.setActivitiesItem(modeler.getActiveId, 'bpmnId', 'userTask_1');
              if (modeler.getActiveId !== 'userTask_1') {
                setNameValue(modeler.getActive, { id: 'userTask_1' });
              }
            } else {
              if (modeler.getActiveId === 'userTask_1') {
                const flowInfo = computed(() => modeler.getFlowInfo);

                const id = ++flowInfo.value.activityMaxId;
                setNameValue(modeler.getActive, { id: `userTask_${id}` });
                modeler.setActivitiesItem(modeler.getActiveId, 'activityId', id);
                modeler.setActivitiesItem(modeler.getActiveId, 'bpmnId', `userTask_${id}`);
              }
              switch (e) {
                case 1: //任务类型
                  modeler.setActivitiesItem(modeler.getActiveId, 'notifyInitiatorFlag', 1);
                  break;
                case 2: //通知类型
                  modeler.setActivitiesItem(modeler.getActiveId, 'notifyInitiatorFlag', 0);
                  break;
              }
            }

            modeler.setActivitiesItem(modeler.getActiveId, 'activityType', e);

            const currentResponseChoices = modeler.getNodeItem.baseInfo.responseChoices;
            const newData =
              e == 1 ? (currentResponseChoices ? currentResponseChoices : '同意;拒绝') : '';
            modeler.setActivitiesItem(modeler.getActiveId, 'baseInfo.responseChoices', newData);

            values.formActionType &&
              values.formActionType.setFieldsValue({
                notifyInitiatorFlag: modeler.nodeItem?.notifyInitiatorFlag,
                'baseInfo.responseChoices': newData,
              });
          }, 20);
        },
      };
    },
  },
  {
    field: `notifyInitiatorFlag`,
    component: 'Switch',
    label: '处理完成后是否通知发起人',
    colProps: {
      span: 24,
    },
    ifShow: ({ values }) => {
      return Boolean(values.activityType);
    },
    componentProps: {
      checkedValue: 1,
      unCheckedValue: 0,
      onChange: (e) => {
        setTimeout(() => {
          modeler.setActivitiesItem(modeler.getActiveId, 'notifyInitiatorFlag', e);
        });
      },
    },
  },
  {
    field: 'selectUserNum',
    label: '选择次数',
    component: 'InputNumber',
    colProps: {
      span: 12,
    },
    componentProps: {
      onChange: (e) => {
        setTimeout(() => {
          modeler.setActivitiesItem(modeler.getActiveId, 'selectUserNum', e);
        });
      },
    },
  },
  {
    field: 'maxLimitedNum',
    label: '最大限制人数',
    component: 'InputNumber',
    colProps: {
      span: 12,
    },
    componentProps: {
      onChange: (e) => {
        setTimeout(() => {
          modeler.setActivitiesItem(modeler.getActiveId, 'maxLimitedNum', e);
        });
      },
    },
  },
  {
    field: 'fieldPerms',
    label: '',
    component: 'Select',
    colProps: {
      span: 24,
    },
    itemProps: {
      autoLink: false,
    },
    render() {
      return <FieldPerms />;
    },
  },
  {
    field: 'transRoles',
    label: '',
    component: 'Select',
    colProps: {
      span: 24,
    },
    itemProps: {
      autoLink: false,
    },
    render() {
      return <OtherSettings />;
    },
  },
];
export const formSchema: Recordable[] = [
  {
    field: 'label',
    label: '字段名',
  },
  {
    field: 'field',
    label: '字段编码',
  },
  {
    field: 'updateType',
    label: '权限',
    required: true,
    component: 'RadioGroup',
    componentProps: {
      defaultValue: 1,
      options: UpdateTypeList,
    },
  },
];
export const condition = {
  conditionId: 12,
  conditionType: 1,
  children: [
    {
      conditionId: 39,
      conditionType: 2,
      children: [
        {
          conditionId: 40,
          conditionType: 0,
          children: null,
          activityFNType: 0,
          responseChoice: '',
          compareType: 0,
          compareContent: '',
          tumTrfType: 0,
          sourceActivityId: 1,
          bindPropName: '',
        },
        {
          conditionId: 41,
          conditionType: 0,
          children: null,
          activityFNType: 0,
          responseChoice: '',
          compareType: 0,
          compareContent: '1',
          tumTrfType: 0,
          sourceActivityId: -1,
          bindPropName: '拟稿人重复标识',
        },
      ],
      activityFNType: 0,
      responseChoice: '',
      compareType: 0,
      compareContent: '',
      tumTrfType: 0,
      sourceActivityId: -1,
      bindPropName: '',
    },
  ],
  activityFNType: 0,
  responseChoice: '',
  compareType: 0,
  compareContent: '',
  tumTrfType: 0,
  sourceActivityId: -1,
  bindPropName: '',
};
export const conditionEnd = {
  conditionId: 17,
  conditionType: 2,
  children: [
    {
      conditionId: 18,
      conditionType: 0,
      children: null,
      activityFNType: 0,
      responseChoice: '拒绝',
      compareType: 0,
      compareContent: '',
      tumTrfType: 0,
      sourceActivityId: 5,
      bindPropName: '',
    },
  ],
  activityFNType: 0,
  responseChoice: '',
  compareType: 0,
  compareContent: '',
  tumTrfType: 0,
  sourceActivityId: -1,
  bindPropName: '',
};
